#! /usr/bin/python
# -*- coding: utf-8 -*-

import os;

def write2cpp(file_name):
    i_file = open(file_name, "rb")

    (base_file_name, ext)= os.path.splitext(file_name)
    base_file_name = os.path.basename(base_file_name)
    
    o_file_hpp = open(base_file_name+".hpp", "w")
    inc_guard = ("%s_HPP_INCLUDED") % base_file_name.upper()
    o_file_hpp.write('\n#ifndef %s\n' % inc_guard)
    o_file_hpp.write('#define %s\n\n' % inc_guard)
    o_file_hpp.write('#include <cstddef>\n\n')
    o_file_hpp.write('class %s {\n' % base_file_name.capitalize())
    o_file_hpp.write('public:\n')
    o_file_hpp.write('    static const char* getData() {\n')
    o_file_hpp.write('         return data;\n')
    o_file_hpp.write('    }\n')
    o_file_hpp.write('    static std::size_t getSize() {\n')
    o_file_hpp.write('         return dataSize;\n')
    o_file_hpp.write('    }\n')
    o_file_hpp.write('private:\n')
    o_file_hpp.write('    static const char data[];\n')
    o_file_hpp.write('    static const std::size_t dataSize;\n')
    o_file_hpp.write('};\n\n')
    o_file_hpp.write('#endif // %s\n\n' % inc_guard)
    o_file_hpp.close()
    
    o_file_cpp = open(base_file_name+".cpp", "w")
    o_file_cpp.write('\n#include "%s"\n\n' % (base_file_name+".hpp"))
    o_file_cpp.write('const char %s::data[] = {\n' % base_file_name.capitalize())

    i = 1
    while True:
        if i % 20 == 0:
            o_file_cpp.write('\n')
        o = i_file.read(1)
        if not o:
            break
        o_file_cpp.write("%s, " % hex(ord(o)) )
        i = i + 1

    o_file_cpp.write('0x0 };\n\n')

    o_file_cpp.write('const std::size_t %s::dataSize = %s;\n\n' % (base_file_name.capitalize(),i-1) )
    o_file_cpp.write('// EOF\n\n')
                      
    o_file_cpp.close()
    
    i_file.close()


write2cpp("test.dat")
